(function(){
var AFG = (function(){
	var gmail;
	var cont;
	var showed = false;
	var gwidth;
	var timer = false;
	
	return {
		ready: function(func){
			$(document).ready(function(){
				var cf = $('#canvas_frame');
				if(cf.length > 0){
					frame_doc = cf[0].contentDocument;
					$(frame_doc).ready(function(){	
						setTimeout(function(){
							cont = $('<div id="astrid-for-gmail-conteiner"></div>');
							
							cont.css({
								"position":"absolute",
								"right":"0px",
								"background":"#f1f1f1",
								"top":"159px",
								"width":"210px",
								"height":"100px",
								"border-left": "1px solid #e5e5e5",
								"border-top": "1px solid #e5e5e5",
								"display":"none"
							});
							
							var frame_body = $('body', frame_doc);
							frame_body.append(cont);
							gmail = $($('.no .nn', frame_body)[3]);
							gwidth = parseInt(gmail.width());
							func();
						}, 1000);
					});
				}
			});
		},
		
		show: function(){
			
			
			cont.css('display','block');
			cont.height( gmail.height() );
			gmail.width( gwidth - 210 );
			showed = true;
		},
		
		hide: function(){

			cont.css('display','none');
			gmail.width(  gwidth );
			showed = false;
		},
		
		isShowed: function(){
			return showed;
		},
		
		onResize: function(){
			gwidth = parseInt( gmail.width() );
			if(document.location.hash == '#inbox'){
				cont.css('display','none');
				if(timer) clearTimeout(timer);
				timer = setTimeout(function(){
					AFG.show();
				}, 1000);
			}
		}
	}
}());

AFG.ready(function(){
	if(document.location.hash == '#inbox'){
		AFG.show();
	}
	
	$(window).bind('hashchange', function(){
		if(document.location.hash == '#inbox'){
			AFG.show();
		}else{
			AFG.hide();
		}
	});
	
	$(window).resize(function() {
		AFG.onResize();
	});
});
})();
